package com.rayc.mall.api.config;

import com.rayc.mall.common.context.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Ray-C
 * @CreateTime: 2025-03-25
 * @Description: OpenFeign配置类
 * @Version: 1.0
 */
@Configuration
public class FeignConfig {
    /**
     * 定义feign所输出的日志等级
     */
    @Bean
    public Logger.Level loggerLevel() {
        return Logger.Level.FULL;
    }


    /**
     * 注册feign的拦截器
     */
    @Bean
    public RequestInterceptor userInfoRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 获取登录用户
                Long userId = UserContext.getUser();
                if (userId == null) {
                    // 如果为空则直接跳过
                    return;
                }
                // 如果不为空则放入请求头中，传递给下游微服务
                template.header("user-id", userId.toString());
            }
        };
    }
}
